import * as Cesium from '@cesium/Source/Cesium.js';

function distanceCartographic(point1cartographic, point2cartographic) {
  var geodesic = new Cesium.EllipsoidGeodesic();
  geodesic.setEndPoints(point1cartographic, point2cartographic);
  var distance = geodesic.surfaceDistance;
  //console.log(Math.sqrt(Math.pow(distance, 2) + Math.pow(endheight, 2)));
  //返回两点之间的距离
  distance = Math.sqrt(Math.pow(distance, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
  return distance;
}

function distanceCartesian(point1, point2) {
  var point1cartographic = Cesium.Cartographic.fromCartesian(point1);
  var point2cartographic = Cesium.Cartographic.fromCartesian(point2);
  return distanceCartographic(point1cartographic, point2cartographic)
}

//空间两点距离计算函数
function getSpaceDistance(positions) {
  var dis = 0;
  for (var i = 0; i < positions.length - 1; i++) {
    var s = distanceCartesian(positions[i], positions[i + 1]);
    dis = dis + s;
  }
  return dis.toFixed(2);
}

function getTerrainDistance(point1cartographic, point2cartographic) {
  var geodesic = new Cesium.EllipsoidGeodesic();
  geodesic.setEndPoints(point1cartographic, point2cartographic);
  var s = geodesic.surfaceDistance;
  var cartoPts = [point1cartographic];
  for (var jj = 1000; jj < s; jj += 1000) { //分段采样计算距离
    var cartoPt = geodesic.interpolateUsingSurfaceDistance(jj);
    cartoPts.push(cartoPt);
  }
  cartoPts.push(point2cartographic);
  //返回两点之间的距离
  var promise = Cesium.sampleTerrain(viewer.terrainProvider, 8, cartoPts);
  Cesium.when(promise, function(updatedPositions) {
    for (var jj = 0; jj < updatedPositions.length - 1; jj++) {
      var geoD = new Cesium.EllipsoidGeodesic();
      geoD.setEndPoints(updatedPositions[jj], updatedPositions[jj + 1]);
      var innerS = geoD.surfaceDistance;
      innerS = Math.sqrt(Math.pow(innerS, 2) + Math.pow(updatedPositions[jj + 1].height - updatedPositions[jj].height, 2));
      distance += innerS;
    }
    //在三维场景中添加Label
    var lon1 = viewer.scene.globe.ellipsoid.cartesianToCartographic(labelPt).longitude;
    var lat1 = viewer.scene.globe.ellipsoid.cartesianToCartographic(labelPt).latitude;
    var lonLat = "(" + Cesium.Math.toDegrees(lon1).toFixed(2) + "," + Cesium.Math.toDegrees(lat1).toFixed(2) + ")";
    var textDisance = distance.toFixed(2) + "米";
    if (distance > 10000)
      textDisance = (distance / 1000.0).toFixed(2) + "千米";
    floatingPoint = viewer.entities.add({
      name: '贴地距离',
      position: labelPt,
      point: {
        pixelSize: 5,
        color: Cesium.Color.RED,
        outlineColor: Cesium.Color.WHITE,
        outlineWidth: 2,
      },
      label: {
        text: lonLat + textDisance,
        font: '18px sans-serif',
        fillColor: Cesium.Color.GOLD,
        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
        outlineWidth: 2,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        pixelOffset: new Cesium.Cartesian2(20, -20),
      }
    });
  });
}

/*方向*/
function Bearing(from, to) {
  var lat1 = from.lat * radiansPerDegree;
  var lon1 = from.lon * radiansPerDegree;
  var lat2 = to.lat * radiansPerDegree;
  var lon2 = to.lon * radiansPerDegree;
  var angle = -Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) *
    Math.cos(lat2) * Math.cos(lon1 - lon2));
  if (angle < 0) {
    angle += Math.PI * 2.0;
  }
  angle = angle * degreesPerRadian; //角度
  return angle;
}

export default getSpaceDistance
export {
  distanceCartographic,distanceCartesian,
  getSpaceDistance,
  getTerrainDistance
}
